import scrapy

from ScrapyObject.spiders.utils.url_utils import *

'''
scrapy crawl ltad -o ltad.json
https://18uj.life/vod
'''


class LtadSpider(scrapy.Spider):
    name = "ltad"
    # 前缀
    prefix = 'https://'
    # 中缀
    website = '18uj'
    # 后缀
    suffix = '.life/'
    allowed_domains = [website + '.life']
    start_urls = [prefix + website + suffix + 't/1']

    def __init__(self):
        self.i = 0

    def parse(self, response):
        # 获取字符串类型的网页内容
        content = get_data(response)
        temp_str = response.xpath("//script[@type='text/javascript']/text()").extract()
        tags = response.xpath("//div[@class='play-tags']//a/text()").extract()
        names = response.xpath("//h1[@class='play-title']/text()").extract()
        if len(temp_str) and len(tags) and len(names):
            video_urls = re.search(r"const source =.*?';", temp_str[0]).group()
            picture_urls = re.search(r"let poster =.*?';", temp_str[0]).group()
            self.i = self.i + 1
            yield get_video_item(id=self.i, tags=tags[0], url='', name=names[0].split("-")[0].strip(), pUrl=picture_urls[14:-2], vUrl=video_urls[16:-2])
        url_list = get_url(content)
        for url in url_list:
            if url.startswith('/'):
                yield scrapy.Request(split_joint(self.prefix + self.website + self.suffix, url), callback=self.parse)
            elif url.startswith('http'):
                yield scrapy.Request(url, callback=self.parse)
